Debian 9 Stretch
Sponsored Link

Docker : Use Persistent Storage
2017/08/03
 
When Container is removed, data in it are also lost, so it's necessary to use external filesystem in Container as persistent storage if you need.
[1] For exmaple, create a Container only for using to save data as a storage server with an image busybox.
root@dlp:~#
vi Dockerfile
# create new

FROM busybox
MAINTAINER ServerWorld <admin@srv.world>

VOLUME /storage
CMD /bin/sh

# build image

root@dlp:~#
docker build -t storage .
root@dlp:~#
docker images

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
storage                 latest              e5f465d60931        24 seconds ago      1.13MB
web_server              latest              d6df7acdc0e3        7 minutes ago       296MB
srv.world/deb_apache2   latest              8efc6944bf8d        2 days ago          219MB
debian                  latest              a20fd0d59cf1        10 days ago         100MB
busybox                 latest              efe10ee6727f        2 weeks ago         1.13MB

# generate a Container with any name you like

root@dlp:~#
docker run -it --name storage_server storage

/ #
exit
[2] To use the Container above as a Storage Server from other Containers, add an option [--volumes-from].
root@dlp:~#
docker run -it --name debian_server --volumes-from storage_server debian /bin/bash
root@d9d1f5e41206:/#
df -hT

Filesystem                  Type     Size  Used Avail Use% Mounted on
overlay                     overlay   71G  2.0G   65G   3% /
tmpfs                       tmpfs    4.0G     0  4.0G   0% /dev
tmpfs                       tmpfs    4.0G     0  4.0G   0% /sys/fs/cgroup
/dev/mapper/debian--vg-root ext4      71G  2.0G   65G   3% /storage
shm                         tmpfs     64M     0   64M   0% /dev/shm
tmpfs                       tmpfs    4.0G     0  4.0G   0% /sys/firmware

root@d9d1f5e41206:/#
echo "persistent storage" >> /storage/testfile.txt

root@d9d1f5e41206:/#
ls -l /storage

total 4
-rw-r--r-- 1 root root 19 Aug  4 02:49 testfile.txt
[3] Make sure datas are saved to run a Container of Storage Server like follows.
root@dlp:~#
docker start storage_server

root@dlp:~#
docker exec -it storage_server cat /storage/testfile.txt

persistent storage
[4] For other way to save data in external filesystem, it's possible to mount a directory on Docker Host into Containers.
# create a directory

root@dlp:~#
mkdir -p /var/lib/docker/disk01

root@dlp:~#
echo "persistent storage" >> /var/lib/docker/disk01/testfile.txt
# run a Container with mounting the directory above on /mnt

root@dlp:~#
docker run -it -v /var/lib/docker/disk01:/mnt debian /bin/bash
root@aea7f8b758d4:/#
df -hT

Filesystem                  Type     Size  Used Avail Use% Mounted on
overlay                     overlay   71G  2.0G   65G   3% /
tmpfs                       tmpfs    4.0G     0  4.0G   0% /dev
tmpfs                       tmpfs    4.0G     0  4.0G   0% /sys/fs/cgroup
/dev/mapper/debian--vg-root ext4      71G  2.0G   65G   3% /mnt
shm                         tmpfs     64M     0   64M   0% /dev/shm
tmpfs                       tmpfs    4.0G     0  4.0G   0% /sys/firmware

root@aea7f8b758d4:/#
cat /mnt/testfile.txt

persistent storage
 
Tweet